// packageB/useraftersales/index.js
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        order: {},
        detail: {},
        detailinfo:[],
        resourceURL: "",
        fileList: [],
        content: "",
        images: [],
        id:'',
        resourceURL: `${wx.api.host}/`,
        columns: ['未收到','已收到'],
        index:'',
        value:'请选择 >'
    },

    onLoad(options) {
        this.setData({
            id:options.order_id
        })
        // console.log(options);
        this.getorderlist()
    },
    
    onConfirm(event) {
        const { picker, value, index } = event.detail;
        // Toast(`当前值：${value}, 当前索引：${index}`);
       this.setData({
           index:index+1,
           show: false,
           value:value
       })
    //    console.log(this.data.index);
      },
    
      onCancel() {
        this.setData({ show: false });
      },
      showPopup() {
        this.setData({ show: true });
      },

    //获取订单列表
    getorderlist() {
        wx.http.get(wx.api.getorderdetail, {
            order_id: this.data.id
        }).then((res) => {
            this.setData({
                order: res.data.order,
                detail: res.data.productinfo[0].detailinfo
            })
            // console.log(this.data.detail);
        })
    },
    //上传文件
    uploadtheimage(event) {
        const {
            file
        } = event.detail;

        var that = this;
        wx.uploadFile({
            url: wx.api.host + '/h5/uploadshopimage',
            filePath: file.url,
            name: 'image',
            formData: {
                type_name: 'after'
            },
            header: {
                token: wx.getStorageSync('token')
            },
            success(res) {
                // 上传完成需要更新 fileList
                if (res.statusCode != 200) {
                    wx.showToast({
                        title: '上传失败',
                        icon: 'none'
                    })
                }
                let data = JSON.parse(res.data)
                let url = {
                    url: `${wx.api.host}/${data.data.file_path}`,
                }

                let fileList = that.data.fileList
                fileList.push(url)
                let images = that.data.images
                images.push(data.data.file_path)

                that.setData({
                    fileList,
                    images
                })
                console.log(that.data.fileList);
            },
        });

    },
    //删除图片
    deleteImage(event) {
        // console.log(event.detail.index);
        let fileList = this.data.fileList
        fileList.splice(event.detail.index, 1);
        let images = this.data.images
        images.splice(event.detail.index, 1);
        this.setData({
            fileList,
            images
        })
    },
    // 提交
    useraftersales() {
        Toast.loading({
            message: '加载中...',
            forbidClick: true
        })
    //    console.log(this.data.images);
        wx.http.post(wx.api.useraftersales, {
            order_id: this.data.order.order_id,
            detail_id: this.data.detail[0].detail_id,
            content: this.data.content,
            images: this.data.images,
            is_status:this.data.index
        },).then((res) => {
            Toast.clear()
            if (res.code == 1) {
                Toast.success({
                    message: '提交售后成功',
                    duration: 1000
                });
                this.SuccessJump()
            }
        })
    },
    onChange(event) {

        this.setData({
            content: event.detail
        })
    },

    SuccessJump(){
        wx.navigateTo({
            url: '../AfterSalesSuccess/index?id='+this.data.id
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
   

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})